/*
 * @Author: szx
 * @Date: 2022-02-12 14:13:46
 * @LastEditTime: 2022-02-12 14:38:37
 * @Description:
 * @FilePath: \leetcode\1400-1499\1405\1405.js
 */
/**
 * @param {number} a
 * @param {number} b
 * @param {number} c
 * @return {string}
 */
var longestDiverseString = function (a, b, c) {
    const res = [];
    const arr = [
        [a, 'a'],
        [b, 'b'],
        [c, 'c']
    ];

    while (true) {
        // 降序排序
        arr.sort((a, b) => b[0] - a[0]);
        let has = false;
        for (const [i, [cnt, ch]] of arr.entries()) {
            //如果数量为0，那么就break，因为后面的肯定是小于等于0的
            if (cnt <= 0) break;
            const m = res.length;
            // 如果前两个字符都是相同的，那么就跳过，换下一个字符尝试
            if (m >= 2 && res[m - 1] == ch && res[m - 2] == ch) continue;
            res.push(ch);
            arr[i][0]--;
            has = true;
            break;
        }
        if (!has) {
            break;
        }
    }
    return res.join('');
};
console.log(longestDiverseString(1, 1, 7));
